<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>数据填报</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			$.extend($.validator.messages, {
				remote: "参数重复",
				});
			$("#name").focus();
			var validator = $("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox") || element.is(":radio") || element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			
			$(".unique").each(function(){
				var name = $(this).attr("name");
				var index = $(this).attr("ind");
				var pre = name.split('.')[0];
				$(this).rules("add",{
					remote: {
					    url: $("#ctx").val()+"/ins/insData/unique",     //后台处理程序
					    type: "get",               //数据发送方式
					    async:false,
					    data: {                     //要传递的数据
					        pre: function() {
					            return pre;
					        },
					        id: function() {
					        	return $("#did"+index).val();
					        },
					        fieldId: function() {
					        	return $("#fid"+index).val();
					        }
					    }
					}
				});
			});
		});
		function submitAll(){
			var url = "${ctx}/ins/insData/save";
            $.ajax({
                type: 'POST',
                url : url,
                async:false,
                data: $('#inputForm').serialize(),             //获取表单数据
                success : function(data) {
                	if(data != "success"){
                	    flag = false;
                        alert(data);
                	}else{
                	    flag = true;
                        alert("保存成功");
                        parent.$.jBox.close(true);
                        parent.$('#btnSubmit').click();
                	}
                        return flag;
                },
                error : function(data){
                	alertx("无权限");
                }
            });
		}
		function submitForm(){                                      //子窗体页面的保存数据方法 通过ajax
			var flag = false;
		    var suc = $("#inputForm").valid();
            if(suc){
				return servicePassword(submitAll,$("#username").val());
            }
            return flag;
        }
	</script>
	<style type="text/css">
		.overRange{
			border-color:red
		}
	</style>
</head>
<body>
	<br/>
	<form:form id="inputForm" modelAttribute="insData" action="${ctx}/ins/insData/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<input  type="hidden"  id="ctx" value="${ctx}"/>
		<input  type="hidden"  id="username" value="${fns:getUser().name}"/>
		<input type="hidden" value="${insData.table.id}" name="table.id"/>
		<sys:message content="${message}"/>		
		<%-- <div class="control-group">
			<label class="control-label">名称：</label>
			<div class="controls">
				<form:input path="name" htmlEscape="false" maxlength="64" class="input-xlarge required"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">排序：</label>
			<div class="controls">
				<form:input path="sort" htmlEscape="false" class="input-xlarge  digits"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">备注：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="1000" class="input-xxlarge "/>
			</div>
		</div> --%>
		<c:forEach items="${insData.datas}" var="dataLink" varStatus="status">
			<div class="control-group" <c:if test="${dataLink.field.preInput == 2}">style="display:none;"</c:if>  >
				<label class="control-label">${dataLink.field.name}：</label>
				<div class="controls">
					<c:choose>
						<c:when test="${dataLink.field.preInput==1}">
							<select style="width:48%" name="datas[${status.index}].value" data-value="${dataLink.value}" class="<c:if test="${dataLink.field.uniquely==1}">unique</c:if> input-medium <c:if test="${dataLink.field.required==1}">required</c:if>">
								<option value="">---请选择---</option>
								<c:forEach items="${dataLink.field.insValueList}" var="val">
									<option value="${val}" <c:if test="${dataLink.value==val}">selected</c:if>>${val}</option>
								</c:forEach>
							</select>
						</c:when>
						<c:otherwise>
							<c:choose>
								<c:when test="${dataLink.field.type==2}">
									<input onkeyup="value=value.replace(/\.\d{${dataLink.field.digit},}$/,value.substr(value.indexOf('.'),${dataLink.field.digit+1}))" <c:if test="${dataLink.overRange=='1'}">style="border-color:red"</c:if> type="text" class="<c:if test="${dataLink.field.uniquely==1}">unique</c:if> input-xlarge number <c:if test="${dataLink.field.required==1}">required</c:if>" value="${empty dataLink.value?dataLink.field.defaultValue:dataLink.value}" name="datas[${status.index}].value"/>
								</c:when>
								<c:when test="${dataLink.field.type==3}">
									<input name="datas[${status.index}].value" type="text" readonly="readonly" maxlength="22" class="<c:if test="${dataLink.field.uniquely==1}">unique</c:if> input-medium Wdate <c:if test="${dataLink.field.required==1}">required</c:if>"
									value="${empty dataLink.value?dataLink.field.defaultValue:dataLink.value}" pattern="yyyy年MM月dd日 HH时mm分ss秒"
									onclick="WdatePicker({dateFmt:'yyyy年MM月dd日 HH时mm分ss秒',isShowClear:false});"/>
								</c:when>
								<c:when test="${dataLink.field.type==4}">
									<input name="datas[${status.index}].value" type="text" readonly="readonly" maxlength="20" class="<c:if test="${dataLink.field.uniquely==1}">unique</c:if> input-medium Wdate <c:if test="${dataLink.field.required==1}">required</c:if>"
									value="${empty dataLink.value?dataLink.field.defaultValue:dataLink.value}" pattern="yyyy年MM月dd日"
									onclick="WdatePicker({dateFmt:'yyyy年MM月dd日',isShowClear:false});"/>
								</c:when>
								<c:when test="${dataLink.field.type==5}">
									<input name="datas[${status.index}].value" type="text" readonly="readonly" maxlength="20" class="<c:if test="${dataLink.field.uniquely==1}">unique</c:if> input-medium Wdate <c:if test="${dataLink.field.required==1}">required</c:if>"
									value="${empty dataLink.value?dataLink.field.defaultValue:dataLink.value}" pattern="yyyy年MM月"
									onclick="WdatePicker({dateFmt:'yyyy年MM月',isShowClear:false});"/>
								</c:when>
								<c:otherwise>
									<input type="text" id="datas[${status.index}].value" ind="${status.index}" class="<c:if test="${dataLink.field.uniquely==1}">unique</c:if> input-xlarge <c:if test="${dataLink.field.required==1}">required</c:if>" value="${empty dataLink.value?dataLink.field.defaultValue:dataLink.value}" name="datas[${status.index}].value"/>
								</c:otherwise>
							</c:choose>
						</c:otherwise>
					</c:choose>
					<input type="hidden" value="${insData.table.id}" name="datas[${status.index}].table.id"/>
					<input type="hidden" value="${insData.id}" name="datas[${status.index}].data.id" id="did${status.index}"/>
					<input type="hidden" value="${dataLink.field.id}" name="datas[${status.index}].field.id" id="fid${status.index}"/>
					<input type="hidden" value="${dataLink.field.name}" name="datas[${status.index}].field.name"/>
					<input type="hidden" value="${dataLink.field.type}" name="datas[${status.index}].field.type"/>
					<input type="hidden" value="${dataLink.field.sort}" name="datas[${status.index}].field.sort"/>
					<input type="hidden" value="${dataLink.field.preValues}" name="datas[${status.index}].field.preValues"/>
				</div>
			</div>
		</c:forEach>
	</form:form>
</body>
</html>